package com.ming.stringstudy.feedbacksystem.controller;

import com.ming.stringstudy.feedbacksystem.entity.RolePermission;
import com.ming.stringstudy.feedbacksystem.pojo.PageBean;
import com.ming.stringstudy.feedbacksystem.pojo.Result;
import com.ming.stringstudy.feedbacksystem.service.IRolePermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author aming
 * @since 2025-01-04
 */
@RestController
@RequestMapping("/rolePermission")
public class RolePermissionController {
    @Autowired
    private IRolePermissionService rolePermissionService;

    @GetMapping
    public Result page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize) {
        try {
            PageBean pageBean = rolePermissionService.page(page, pageSize);
            return Result.success(pageBean);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("服务器内部错误");
        }
    }

    @PostMapping
    public Result addRolePermission(@RequestBody RolePermission rolePermission) {
        try {
            int result = rolePermissionService.addRolePermission(rolePermission);
            if (result == 0) {
                return Result.error("添加失败");
            } else {
                return Result.success("添加成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("服务器内部错误");
        }
    }

    @PutMapping
    public Result updateRolePermission(@RequestBody RolePermission rolePermission) {
        try {
            int result = rolePermissionService.updateRolePermission(rolePermission);
            if (result == 0) {
                return Result.error("修改失败");
            } else {
                return Result.success("修改成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("服务器内部错误");
        }
    }

    @DeleteMapping("/{ids}")
    public Result deleteRolePermission(@PathVariable List<Integer> ids) {
        try {
            int result = rolePermissionService.deleteRolePermission(ids);
            if (result == 0) {
                return Result.error("删除失败");
            } else {
                return Result.success("删除成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("服务器内部错误");
        }
    }


}
